package cn.anquing.jiami.messagedigest;

import com.alibaba.fastjson.JSON;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.lang.StringUtils;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * <pre>
 *
 * @description
 * @author wangqq
 * @email wangqq65@gdii-yueyun.com
 * @since 2023/4/20 14:12
 * @copyright 广东美云智数科技有限公司
 * </pre>
 */
public class MipDataEncrypt {
    private static int DEFAULT_HASH_COUNT = 1;

    public MipDataEncrypt() {
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String salt = "15b44d1d-1c9f-4fb0-9f9a-b436134c6c95";
//        String info = "{\"fdModelName\":\"GPM-HY-SELF\",\"fdWorkitemId\":\"RN20230420145346664\",\"fdFlowId\":\"RDM,TSK,bcbdb6b1-022d-4245-a6c2-38d029041382\",\"userAccount\":[\"luowc12\"],\"fdKey\":\"GPM-HY-SELF\",\"isDelTodoUsers\":0,\"fdType\":1}";
        String info = "{\n" +
                "    \"fdDocTime\": 1685433225000,\n" +
                "    \"fdWorkitemId\": \"NR20230420100807268\",\n" +
                "    \"fdAuthName\": \"liangjj101\",\n" +
                "    \"fdFlowId\": \"RDM,TSK,8b311aa2-9a3e-4697-b992-8eeb9e28801e2\",\n" +
                "    \"docSubject\": {\n" +
                "        \"en-US\": \"测试蓝凌OA待办0420\",\n" +
                "        \"zh-CN\": \"测试蓝凌OA待办0420\",\n" +
                "        \"ja-JP\": \"测试蓝凌OA待办0420\"\n" +
                "    },\n" +
                "    \"fdKey\": \"GPM-HY-SELF\",\n" +
                "    \"fdLink\": \"http://10.23.114.211:81/#/bridge?identifier=mip&objectType=TSK&objectId=8b311aa2-9a3e-4697-b992-8eeb9e28801e2&mobile_templateId=TSK\",\n" +
                "    \"type\": \"todo\",\n" +
                "    \"fdType\": \"1\",\n" +
                "    \"fdModelName\": \"GPM-HY-SELF\",\n" +
                "    \"fdDocSubject\": {\n" +
                "        \"en-US\": \"测试蓝凌OA待办0420\",\n" +
                "        \"zh-CN\": \"测试蓝凌OA待办0420\",\n" +
                "        \"ja-JP\": \"测试蓝凌OA待办0420\"\n" +
                "    },  \n" +
                "    \"defaultLanguage\": \"zh-CN\",\n" +
                "    \"fdLongContent\": {\n" +
                "        \"en-US\": \"测试蓝凌OA待办0420\",\n" +
                "        \"zh-CN\": \"测试蓝凌OA待办0420\",\n" +
                "        \"ja-JP\": \"测试蓝凌OA待办0420\"\n" +
                "    },\n" +
                "      \"fdSendersAccount\": {\n" +
                "        \"zh-CN\": [\n" +
                "            \"liangjj101\"\n" +
                "        ]\n" +
                "    },\n" +
                "    \"fdIsMobile\": false\n" +
                "}";

        byte[] data = JSON.parseObject(info).toString().getBytes("UTF-8");
//        byte[] data = info.getBytes("UTF-8");


        String token =  MipDataEncrypt.encryptByte(data,salt);
        String encode = URLEncoder.encode(token, "utf-8");
        System.out.println("token:"+ token);
        System.out.println("encode:"+ encode);
    }

    public static String getEncryptStr(String data, String salt) {
        if (StringUtils.isBlank(data)) {
            return null;
        } else {
            try {
                byte[] bytesSalts = StringUtils.isNotBlank(salt) ? salt.getBytes("UTF-8") : null;
                byte[] bytesData = StringUtils.isNotBlank(data) ? data.getBytes("UTF-8") : null;
                byte[] encryptStr = md5hash(bytesData, bytesSalts, DEFAULT_HASH_COUNT);
                return byteToStr(encryptStr);
            } catch (Exception var5) {
                System.out.println("数据加密失败 , data:" + data + ",salt:" + salt);
                var5.printStackTrace();
                return null;
            }
        }
    }

    public static String getEncryptStr(String data, String salt, int hashCount) {
        if (StringUtils.isBlank(data)) {
            return null;
        } else {
            try {
                byte[] bytesSalts = StringUtils.isNotBlank(salt) ? salt.getBytes("UTF-8") : null;
                byte[] bytesData = StringUtils.isNotBlank(data) ? data.getBytes("UTF-8") : null;
                byte[] encryptStr = md5hash(bytesData, bytesSalts, hashCount);
                return byteToStr(encryptStr);
            } catch (Exception var6) {
                System.out.println("数据加密失败 , data:" + data + ",salt:" + salt);
                var6.printStackTrace();
                return null;
            }
        }
    }

    public static String encryptByte(byte[] encryptBuff, String salt, int hashCount) {
        if (null != encryptBuff && encryptBuff.length != 0) {
            try {
                byte[] bytesSalts = StringUtils.isNotBlank(salt) ? salt.getBytes("UTF-8") : null;
                byte[] encryptStr = md5hash(encryptBuff, bytesSalts, hashCount);
                return byteToStr(encryptStr);
            } catch (Exception var5) {
                System.out.println("数据加密失败 ,salt:" +  salt);
                var5.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String encryptByte(byte[] encryptBuff, String salt) {
        if (null != encryptBuff && encryptBuff.length != 0) {
            try {
                byte[] bytesSalts = StringUtils.isNotBlank(salt) ? salt.getBytes("UTF-8") : null;
                byte[] encryptStr = md5hash(encryptBuff, bytesSalts, DEFAULT_HASH_COUNT);
                return byteToStr(encryptStr);
            } catch (Exception var4) {
                System.out.println("数据加密失败 ,salt:" +  salt);
                var4.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    private static byte[] md5hash(byte[] bytesData, byte[] bytesSalts, int hashCount) {
        MessageDigest digest = getDigest("MD5");
        byte[] hashed = new byte[0];
        if (digest != null) {
            if (bytesSalts != null) {
                digest.reset();
                digest.update(bytesSalts);
            }

            hashed = digest.digest(bytesData);

            for(int i = DEFAULT_HASH_COUNT; i < hashCount; ++i) {
                digest.reset();
                hashed = digest.digest(hashed);
            }
        }

        return hashed;
    }

    protected static MessageDigest getDigest(String algorithmName) {
        try {
            return MessageDigest.getInstance(algorithmName);
        } catch (NoSuchAlgorithmException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static String byteToStr(byte[] byteData) throws Exception {
        return DatatypeConverter.printBase64Binary(byteData);
    }
}
